package com.skyolder.milkbee.tool;

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageUtil {
	public static void thumbAvatar(String filename) throws IOException {
		// java.io.File pFile = new java.io.File(file);

		java.io.File fo = new java.io.File(filename); // 将要转换出的小图文件
		int nw = 128;
		AffineTransform transform = new AffineTransform();
		BufferedImage buffer = ImageIO.read(fo); // 读取图片
		int width = buffer.getWidth();
		int height = buffer.getHeight();
		int nh = 128;
		double sx = (double) nw / width;
		double sy = (double) nh / height;
		transform.setToScale(sx, sy);
		AffineTransformOp ato = new AffineTransformOp(transform, null);
		BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR);
		ato.filter(buffer, bid);
		String type = filename.substring(filename.indexOf('.') + 1, filename.length());
		// System.out.println("type = " + type);
		ImageIO.write(bid, type, fo);
	}
	public static void thumbLogo(String filename) throws IOException {
		// java.io.File pFile = new java.io.File(file);
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		System.out.println(filename);
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		java.io.File fo = new java.io.File(filename); // 将要转换出的小图文件
		int nw = 94;
		AffineTransform transform = new AffineTransform();
		BufferedImage buffer = ImageIO.read(fo); // 读取图片
		int width = buffer.getWidth();
		int height = buffer.getHeight();
		int nh = 119;
		double sx = (double) nw / width;
		double sy = (double) nh / height;
		transform.setToScale(sx, sy);
		AffineTransformOp ato = new AffineTransformOp(transform, null);
		BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR);
		ato.filter(buffer, bid);
		String type = filename.substring(filename.indexOf('.') + 1, filename.length());
		// System.out.println("type = " + type);
		ImageIO.write(bid, type, fo);
	}
}
